package com.jshcbd.modules.asset.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jshcbd.modules.asset.dto.DisposeDto;
import com.jshcbd.modules.asset.entity.DisposeEntity;
import com.jshcbd.modules.asset.mapper.AssetMapper;
import com.jshcbd.modules.asset.mapper.HandleMapper;
import com.jshcbd.modules.asset.service.IDisposeService;
import com.jshcbd.modules.asset.vo.AssetDisposeVo;
import com.jshcbd.modules.asset.vo.AssetVo;
import com.jshcbd.modules.asset.vo.DisposeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhoucd
 * @date
 * @description
 */
@Slf4j
@Service
@Transactional
public class DisposeServiceImpl extends ServiceImpl<HandleMapper, DisposeEntity> implements IDisposeService {

    @Autowired
    private HandleMapper handleMapper;
    @Autowired
    private AssetMapper assetMapper;


    @Override
    public IPage<DisposeVo> selectPageView(Page<DisposeEntity> page, Wrapper wrapper) {
        IPage<DisposeEntity>  pageResult =  handleMapper.selectPage(page,wrapper);
        QueryWrapper queryWrapper =   new QueryWrapper();
        queryWrapper.in("id",pageResult.getRecords().stream().map(entity -> entity.getAssetId()).toArray());
        List<AssetVo>  assetVoList =  assetMapper.listView(queryWrapper);
        Page<DisposeVo>  returnPage = new Page<>();
        BeanUtils.copyProperties(pageResult,returnPage);
        List<DisposeVo> dataList = pageResult.getRecords().stream().map(entity ->{
            DisposeVo vo = new DisposeVo(
                    assetVoList.stream().filter(assetVo -> assetVo.getId().equals(entity.getAssetId())).findFirst().get()
            );
            vo.setDisPhone(entity.getPhone());
            vo.setDisContacts(entity.getContacts());
            vo.setDisType(entity.getType());
            vo.setDisPhoto(entity.getDisPhoto());
            vo.setDisNote(entity.getNote());
            return  vo;
        }).collect(Collectors.toList());
        returnPage.setRecords(dataList);
        return returnPage;
    }

    @Override
    public void doDispose(DisposeDto disposeDto) {
        Arrays.stream(disposeDto.getAssetIds()).forEach(assetId -> {
            DisposeEntity entity = new DisposeEntity(disposeDto);
            entity.setAssetId(assetId);
            handleMapper.insert(entity);
            assetMapper.updateDis(assetId,disposeDto.getDisDate());
        });
    }

    @Override
    public List<AssetDisposeVo> pageViewByAssetId(String assetId) {
        return handleMapper.queryViewByAssetId(assetId);
    }
}
